/*===========================================================================+
 |   Copyright (c) 2001, 2005 Oracle Corporation, Redwood Shores, CA, USA    |
 |                         All rights reserved.                              |
 +===========================================================================+
 |  HISTORY                                                                  |
 +===========================================================================*/
package sza.oracle.apps.ap.oie.print.webui;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;
import oracle.apps.fnd.common.VersionInfo;
import oracle.apps.fnd.framework.OAApplicationModule;
import oracle.apps.fnd.framework.webui.OAControllerImpl;
import oracle.apps.fnd.framework.webui.OAPageContext;
import oracle.apps.fnd.framework.webui.beans.OAWebBean;
import oracle.apps.fnd.framework.webui.beans.form.OASubmitButtonBean;
import oracle.apps.xdo.oa.common.DocumentHelper;
import sza.oracle.apps.ap.oie.print.server.PrintHeaderVOImpl;

/**
 * Controller for ...
 */
public class OIEPrintCO extends OAControllerImpl
{
  public static final String RCS_ID="$Header$";
  public static final boolean RCS_ID_RECORDED =
        VersionInfo.recordClassVersion(RCS_ID, "%packagename%");
        
  private static final String APPLICATION_SHORT_NAME = "SQLSZA";
  private static final String TEMPLATE_NAME = "PAYMENT_PRINT2";
  private int selectCount = 0;

  /**
   * Layout and page setup logic for a region.
   * @param pageContext the current OA page context
   * @param webBean the web bean corresponding to the region
   */
  public void processRequest(OAPageContext pageContext, OAWebBean webBean)
  {
    super.processRequest(pageContext, webBean);
      String javaScript="JAVASCRIP:function getSelectedCount(){\n" + 
      "        var   ingc; \n" + 
      "        var selectCount = 0;\n" + 
      "        document.getElementById('wklEvent').value = ''; \n"+
      "        for   (ingc=0;ingc <document.all.length;ingc++) \n" + 
      "        { \n" + 
      "            if   (document.all(ingc).type=='checkbox')   \n" + 
      "            { \n" + 
      "               if(document.all(ingc).checked){\n" + 
      "                  selectCount = selectCount+1;\n" + 
      "               }\n" + 
      "            } \n" + 
      "        } \n" + 
      "      if (confirm('\\u5df2\\u9009\\u62e9\\u6253\\u5370\\u8bb0\\u5f55\\u6570:'+selectCount)){ \n" + 
      "          document.getElementById('wklEvent').value = 'Y';" +
      "      } else{ \n" + 
      "          return false;\n"+
      "      }\n" + 
      "}";
     pageContext.putJavaScriptFunction("getSelectedCount",javaScript);     
     OASubmitButtonBean  butten = (OASubmitButtonBean)webBean.findChildRecursive("PrintBtn");
     butten.setOnClick("javascript:getSelectedCount()"); 
  }

  /**
   * Procedure to handle form submissions for form elements in
   * a region.
   * @param pageContext the current OA page context
   * @param webBean the web bean corresponding to the region
   */
  public void processFormRequest(OAPageContext pageContext, OAWebBean webBean)
  {
  
    super.processFormRequest(pageContext, webBean);
    OAApplicationModule am = pageContext.getApplicationModule(webBean);
    if(pageContext.getParameter("GetRecBtn")!=null){
         
          String statusInput = pageContext.getParameter("StatusInput");
          String submitPersonId = pageContext.getParameter("SubmitPersonId");
          String submitDateInput = pageContext.getParameter("SubmitDateInput");
          String submitDateTo = pageContext.getParameter("SubmitDateTo");
          String reportTypeInput = pageContext.getParameter("ReportTypeInput");
          String firstApprId = pageContext.getParameter("FirstApprId");
          String firstApprDateInput = pageContext.getParameter("FirstApprDateInput");
          String firstApprDateTo = pageContext.getParameter("FirstApprDateTo");
          String reportHeaderIdInput = pageContext.getParameter("ReportHeaderIdInput");
          String lastApprId = pageContext.getParameter("LastApprId");
          String lastApprDateInput = pageContext.getParameter("LastApprDateInput");
          String lastApprDateTo = pageContext.getParameter("LastApprDateTo");
          String costCenterInput = pageContext.getParameter("CostCenter");
          String paymethodInput = pageContext.getParameter("Paymethod");
          String shenpibegin = pageContext.getParameter("shenpibegin");
          String shenpito = pageContext.getParameter("shenpito");
          Serializable[] parameters = { statusInput,submitPersonId, submitDateInput,submitDateTo,reportTypeInput,firstApprId,firstApprDateInput,
                                            firstApprDateTo,reportHeaderIdInput,lastApprId,lastApprDateInput,lastApprDateTo,costCenterInput,paymethodInput,shenpibegin,shenpito
                                           };
          am.invokeMethod("getPrintRecord",parameters);
      } else if(pageContext.getParameter("PrintBtn") != null){
              if(("Y").equals(pageContext.getParameter("wklEvent"))){
                print(pageContext,am);
            } 
      }
  }
  
  private void createSignatureImage(String relPath,OAApplicationModule am){
      PrintHeaderVOImpl headerVo = (PrintHeaderVOImpl)am.findViewObject("PrintHeaderVO1");
      String reportHeaders = headerVo.getHeaderIds();
      Serializable[] parameters = { reportHeaders, relPath};  
      System.err.println("relPath"+relPath);
      am.invokeMethod("createSignatureImage",parameters);
  }
    
    private void print(OAPageContext pageContext,OAApplicationModule am){
        String str1 = pageContext.getTemporaryImageLocation();
                            String xmlStr = (String)am.invokeMethod("printPayment");
                            String str3 = xmlStr.replaceAll("@###@",str1);
                            System.err.println(str3);
                            ByteArrayOutputStream stream = new ByteArrayOutputStream();
                              try {
                                  stream.write(str3.getBytes());
                              } catch (IOException e) {
                                  // TODO
                              }
                              createSignatureImage(pageContext.getTemporaryImageLocation(),am);
                                 ByteArrayInputStream inputStream = new ByteArrayInputStream(stream.toByteArray());
                                 DocumentHelper.exportDocument(pageContext,
                                                               APPLICATION_SHORT_NAME,
                                                               TEMPLATE_NAME,
                                                               "zh",
                                                               "CN" ,
                                                                inputStream,
                                                                "PDF",
                                                                null);
                                                                
      }
}
